<template>
  <div class="app-container">
    <!--<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="" prop="roundStart">
        <el-date-picker clearable size="small"
          v-model="queryParams.roundStart"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择轮次开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="" prop="roundEnd">
        <el-date-picker clearable size="small"
          v-model="queryParams.roundEnd"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择轮次结束时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mentor:round:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mentor:round:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mentor:round:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mentor:round:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="roundList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学生选择开始时间" align="center" prop="stuStart" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.stuStart}}</span>
        </template>
      </el-table-column>
      <el-table-column label="学生选择结束时间" align="center" prop="stuEnd" width="180">
        <template slot-scope="scope">
          <span>{{scope.row.stuEnd}}</span>
        </template>
      </el-table-column>
      <el-table-column label="导师选择开始时间" align="center" prop="insStart" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.insStart}}</span>
        </template>
      </el-table-column>
      <el-table-column label="导师选择结束时间" align="center" prop="insEnd" width="180">
        <template slot-scope="scope">
          <span>{{scope.row.insEnd}}</span>
        </template>
      </el-table-column>

      <el-table-column label="激活状态" align="center" prop="isActive" width="100">
        <template slot-scope="scope">
         <el-tag type="info" v-if="scope.row.isActive==0">未激活</el-tag>
         <el-tag type="success" v-if="scope.row.isActive==1">已激活</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" align="center" prop="createDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button
            v-if="scope.row.isActive==0"
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="activeRound(scope.row.id)"
            v-hasPermi="['mentor:round:edit']"
          >激活</el-button>

          <el-button
            v-if="scope.row.isActive==1"
            size="mini"
            type="danger"
            icon="el-icon-edit"
            @click="noActive(scope.row.id)"
            v-hasPermi="['mentor:round:edit']"
          >取消激活</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mentor:round:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mentor:round:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改志愿征集轮次信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <el-form-item label="学生选择开始时间" prop="stuStart">
          <el-date-picker clearable size="small"
            v-model="form.stuStart"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="学生选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="学生选择结束时间" prop="stuEnd">
          <el-date-picker clearable size="small"
            v-model="form.stuEnd"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="学生选择结束时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="导师选择开始时间" prop="insStart">
          <el-date-picker clearable size="small"
                          v-model="form.insStart"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择学生选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="导师选择结束时间" prop="insEnd">
          <el-date-picker clearable size="small"
                          v-model="form.insEnd"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择学生选择结束时间">
          </el-date-picker>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRound, getRound, delRound, addRound, updateRound, exportRound,active } from "@/api/mentor/round";
import {noActive} from "../../../api/mentor/round";

export default {
  name: "Round",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 志愿征集轮次信息表格数据
      roundList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stuStart: undefined,
        stuEnd: undefined,
        insStart:undefined,
        insEnd:undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        stuStart: [
          { required: true, message: "学生选择开始时间不能为空", trigger: "blur" }
        ],
        stuEnd: [
          { required: true, message: "学生选择结束时间不能为空", trigger: "blur" }
        ],
        insStart: [
          { required: true, message: "导师选择开始时间不能为空", trigger: "blur" }
        ],
        insEnd: [
          { required: true, message: "导师选择结束时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询志愿征集轮次信息列表 */
    getList() {
      this.loading = true;
      listRound(this.queryParams).then(response => {
        this.roundList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        stuStart: undefined,
        stuEnd: undefined,
        insStart: undefined,
        insEnd: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加志愿征集轮次信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRound(id).then(response => {
        console.log(response.data)
        this.form = response.data;
        this.open = true;
        this.title = "修改志愿征集轮次信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRound(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRound(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除志愿征集轮次信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRound(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有志愿征集轮次信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportRound(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    async activeRound(id){
      const res = await active(id)
      // console.log(res)
      this.getList()
    },
    async noActive(id) {
      const res = await noActive()
      this.getList()
    }
  }
};
</script>
